package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.admin.domain.BikBicycle;
import com.edu.bhu.admin.domain.BikRecord;
import com.edu.bhu.admin.service.IBikBicycleService;
import com.edu.bhu.admin.util.priceUtil;
import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.utils.DateUtils;
import com.edu.bhu.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.service.IBikRecordService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;

/**
 * 租车记录Controller
 * 
 * @author heyinan
 * @date 2022-03-14
 */
@Controller
@RequestMapping("/admin/record")
public class BikRecordController extends BaseController
{
    private String prefix = "admin/record";

    @Autowired
    private IBikRecordService bikRecordService;

    @Autowired
    private IBikBicycleService bikBicycleService;

    @RequiresPermissions("admin:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 打开我的租赁记录列表
     */
    @RequiresPermissions("admin:record:view")
    @GetMapping("/myRecord")
    public String myRecord()
    {
        return prefix + "/myRecord";
    }

    /**
     * 查询租车记录列表
     */
    @RequiresPermissions("admin:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BikRecord bikRecord)
    {
        startPage();
        List<BikRecord> list = bikRecordService.selectBikRecordList(bikRecord);
        return getDataTable(list);
    }

    /**
     * 查询我的租车记录列表
     */
    @RequiresPermissions("admin:record:list")
    @PostMapping("/myList")
    @ResponseBody
    public TableDataInfo myList(BikRecord bikRecord)
    {
        //选择当前用户对应的列表
        SysUser sysUser = ShiroUtils.getSysUser();
        bikRecord.setUserId(sysUser.getUserId());
        startPage();
        List<BikRecord> list = bikRecordService.selectBikRecordList(bikRecord);
        return getDataTable(list);
    }

    /**
     * 导出租车记录列表
     */
    @RequiresPermissions("admin:record:export")
    @Log(title = "租车记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BikRecord bikRecord)
    {
        List<BikRecord> list = bikRecordService.selectBikRecordList(bikRecord);
        ExcelUtil<BikRecord> util = new ExcelUtil<BikRecord>(BikRecord.class);
        return util.exportExcel(list, "租车记录数据");
    }

    /**
     * 新增租车记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存租车记录
     */
    @RequiresPermissions("admin:record:add")
    @Log(title = "租车记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BikRecord bikRecord)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        //添加当前用户id到记录中
        bikRecord.setUserId(sysUser.getUserId());
        //添加当前时间到记录中
        bikRecord.setStartTime(DateUtils.getNowDate());
        //将单车状态置为已出租
        BikBicycle bikBicycle = new BikBicycle();
        bikBicycle.setBikId(bikRecord.getBikId());
        bikBicycle.setRentStatus(2L);
        bikBicycleService.updateBikBicycle(bikBicycle);
        return toAjax(bikRecordService.insertBikRecord(bikRecord));
    }

    /**
     * 修改租车记录
     */
    @RequiresPermissions("admin:record:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        BikRecord bikRecord = bikRecordService.selectBikRecordByRecordId(recordId);
        mmap.put("bikRecord", bikRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存租车记录
     */
    @RequiresPermissions("admin:record:edit")
    @Log(title = "租车记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BikRecord bikRecord)
    {
        return toAjax(bikRecordService.updateBikRecord(bikRecord));
    }

    /**
     * 单车归还
     */
    @RequiresPermissions("admin:record:edit")
    @Log(title = "租车记录", businessType = BusinessType.UPDATE)
    @PostMapping("/returnBik")
    @ResponseBody
    public AjaxResult returnBik(BikRecord bikRecord)
    {
        //拿到相应记录
        bikRecord = bikRecordService.selectBikRecordByRecordId(bikRecord.getRecordId());

        //记录归还时间
        bikRecord.setEndTime(DateUtils.getNowDate());

        //插入总价
        BikBicycle bikBicycle = bikBicycleService.selectBikBicycleByBikId(bikRecord.getBikId());//拿到车辆信息
        BikRecord record = priceUtil.totalPrice(bikRecord,bikBicycle.getPrice());

        //车辆置为已归还
        BikBicycle bike = new BikBicycle();
        bike.setBikId(bikRecord.getBikId());
        bike.setRentStatus(1L);
        bikBicycleService.updateBikBicycle(bike);

        return toAjax(bikRecordService.updateBikRecord(record));
    }

    /**
     * 租赁支付
     */
    @RequiresPermissions("admin:record:edit")
    @Log(title = "租车记录", businessType = BusinessType.UPDATE)
    @PostMapping("/payRecord")
    @ResponseBody
    public AjaxResult payRecord(BikRecord bikRecord)
    {
        bikRecord.setPayStatus(2L);
        return toAjax(bikRecordService.updateBikRecord(bikRecord));
    }

    /**
     * 删除租车记录
     */
    @RequiresPermissions("admin:record:remove")
    @Log(title = "租车记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bikRecordService.deleteBikRecordByRecordIds(ids));
    }
}
